from flask_wtf import FlaskForm
from wtforms import SelectField, StringField, SubmitField, HiddenField
from flask_ckeditor import CKEditorField
from wtforms.validators import DataRequired, Length, Optional, URL
from app.models.category import Category

audio_status = {
    10: '一审提交',
    9: '一审通过',
    8: '二审提交',
    7: '二审通过',
}


class ArticleForm(FlaskForm):
    cid = SelectField('选择栏目',
                      validators=[DataRequired('请选择标签')],
                      coerce=int,
                      choices=[(0, '选择栏目')])
    thumb = HiddenField('缩略图 ', validators=[Optional(), Length(1, 255)])
    video = StringField('视频', validators=[Optional(), Length(1, 255)], render_kw={'placeholder': '视频地址，没有留空'})
    audio = StringField('音频', validators=[Optional(), Length(1, 255)], render_kw={'placeholder': '音频地址，没有留空'})
    title = StringField('标题', validators=[DataRequired('标题不能为空'), Length(1, 255)], render_kw={'placeholder': '输入标题'})
    attachment = StringField('附件', validators=[Optional(), Length(1, 255)])
    url = StringField('跳转链接', validators=[Optional(), URL(message='必须是URL')], render_kw={'placeholder': '文章跳转链接,可不填 (http://www.lsol.com.cn)'})
    content = CKEditorField('文章内容', validators=[DataRequired('不能为空')])
    submit = SubmitField('提交')

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.cid.choices.extend([(i['id'], i['title']) for i in Category.tree()])

    def load(self, data):
        """填充数据
        """
        for k, v in data.__dict__.items():
            if hasattr(self, k):
                self.__dict__[k].data = '' if v is None else v


class UpdateArticleForm(FlaskForm):
    cid = SelectField('选择栏目',
                      validators=[DataRequired('请选择标签')],
                      coerce=int,
                      choices=[(0, '选择栏目')])
    thumb = HiddenField('缩略图 ', validators=[Optional(), Length(1, 255)])
    video = StringField('视频', validators=[Optional(), Length(1, 255)], render_kw={'placeholder': '视频地址，没有留空'})
    audio = StringField('音频', validators=[Optional(), Length(1, 255)], render_kw={'placeholder': '音频地址，没有留空'})
    title = StringField('标题', validators=[DataRequired('标题不能为空'), Length(1, 255)], render_kw={'placeholder': '输入标题'})
    attachment = StringField('附件', validators=[Optional(), Length(1, 255)])
    url = StringField('跳转链接', validators=[Optional(), URL(message='必须是URL')], render_kw={'placeholder': '文章跳转链接,可不填 (http://www.lsol.com.cn)'})
    content = CKEditorField('文章内容', validators=[DataRequired('不能为空')])
    submit = SubmitField('提交')

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.cid.choices.extend([(i['id'], i['title']) for i in Category.tree()])

    def load(self, data):
        """填充数据
        """
        for k, v in data.__dict__.items():
            if hasattr(self, k):
                self.__dict__[k].data = '' if v is None else v


class EditArticleForm(FlaskForm):
    cid = SelectField('选择栏目',
                      validators=[DataRequired('请选择标签')],
                      coerce=int,
                      choices=[(0, '选择栏目')])
    thumb = HiddenField('缩略图 ', validators=[Optional(), Length(1, 255)])
    video = StringField('视频', validators=[Optional(), Length(1, 255)], render_kw={'placeholder': '视频地址，没有留空'})
    audio = StringField('音频', validators=[Optional(), Length(1, 255)], render_kw={'placeholder': '音频地址，没有留空'})
    title = StringField('标题', validators=[DataRequired('标题不能为空'), Length(1, 255)], render_kw={'placeholder': '输入标题'})
    attachment = StringField('附件', validators=[Optional(), Length(1, 255)])
    url = StringField('跳转链接', validators=[Optional(), URL(message='必须是URL')], render_kw={'placeholder': '文章跳转链接,可不填 (http://www.lsol.com.cn)'})
    edit_content = CKEditorField('文章内容', validators=[DataRequired('不能为空')])
    submit = SubmitField('保存编辑')

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.cid.choices.extend([(i['id'], i['title']) for i in Category.tree()])

    def load(self, data):
        """填充数据
        """
        for k, v in data.__dict__.items():
            if hasattr(self, k):
                self.__dict__[k].data = '' if v is None else v
